﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	[Transaction(TransactionMode.Manual)]
	public class EditSpaceCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			Result result;
			try
			{
				IList<Space> list = new List<Space>();
				foreach (ElementId elementId in selection.GetElementIds())
				{
					if (document.GetElementById(elementId) is Space)
					{
						Space item = document.GetElementById(elementId) as Space;
						list.Add(item);
					}
				}
				if (list.Count < 1)
				{
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
					filteredElementCollector.OfCategory(BuiltInCategory.OST_MEPSpaces);
					foreach (Element element in filteredElementCollector)
					{
						Space item2 = element as Space;
						list.Add(item2);
					}
				}
				if (list.Count > 0)
				{
					new EditSpaceInfoGrid(document, list).ShowDialog();
				}
				else
				{
					MessageBox.Show("当前文档没有空间。", "盈建科提示");
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}
	}
}
